namespace EntityFrameworkSample.Data.Migrations
{
    using EnitityFrameworkSample.Data.Models;
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<EntityFrameworkSample.Data.SchoolDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(EntityFrameworkSample.Data.SchoolDbContext context)
        {
            if (!context.Students.Any())
            {
                context.Students.Add(new Student
                {
                    FirstName = "Tim",
                    LastName = "Cool"
                });
                context.Students.Add(new Student
                {
                    FirstName = "Joe",
                    LastName = "Bob"
                });
            }

            context.SaveChanges();

            context.Courses.AddOrUpdate(c => c.Name,
                new Course { Name = "Algebra 1", Code = "A101" },
                new Course { Name = "English 2", Code = "E215" }
            );

            context.Semesters.AddOrUpdate(s => s.SemesterId,
                new Semester { StartDate = DateTime.Parse("2015-01-01"), EndDate = DateTime.Parse("2015-06-01") }
            );

            context.SaveChanges();

            context.SemesterStudentCourses.AddOrUpdate(s => s.SemesterStudentCourseId,
                new SemesterStudentCourse { CourseId = 3, StudentId = 1, SemesterId = 2 },
                new SemesterStudentCourse { CourseId = 3, StudentId = 2, SemesterId = 2 },
                new SemesterStudentCourse { CourseId = 4, StudentId = 2, SemesterId = 2 }
            );

            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }
    }
}
